package com.qf.sl.youshare.tasks;

import android.os.AsyncTask;
import com.qf.sl.youshare.Constants;
import com.qf.sl.youshare.utils.HttpTools;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

/**
 * Created by IDEA
 * User : SL
 * on  2015/2/22.
 * 9:44
 */
public class ShareArticleTask extends AsyncTask<String, Integer, JSONObject> {

    private TaskProcessor processor;

    public ShareArticleTask(TaskProcessor processor) {
        this.processor = processor;
    }

    @Override
    protected JSONObject doInBackground(String... params) {
        JSONObject ret = null;

        String size = "10";

        if (params != null && params.length > 0) {
            size = params[0];
        }

        String url = Constants.SHARE_URL + "?ac=list&size=" + size;

        byte[] bytes = HttpTools.get(url);
        if (bytes != null) {
            try {
                String str = new String(bytes);
                System.out.println("str = " + str);
                ret = new JSONObject(str);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        return ret;
    }

    @Override
    protected void onPostExecute(JSONObject jsonObject) {
        if (processor != null) {
            processor.processResult(jsonObject);
        }
    }
}
